#ifndef _TASK_H
#define _TASK_H

/*****************************************************************************
 * Macros Definitions                                                        *
 *****************************************************************************/

/* States */
#define  WAITING		0 
#define  READY			1 
#define  SUSPENDED	2 
#define  RUNNING		3 

/* Asssembly Vars Directions */ 
#define spAddH    0x501
#define spAddL    0x502
#define aAdd      0x503
#define hAdd      0x584
#define xAdd      0x585
#define ccrAdd    0x586
#define spIntH    0x587
#define spIntL    0x588
#define aInt      0x589
#define hInt      0x58A
#define xInt      0x58B
#define ccrInt    0x58C
#define varCheck  0x58D

/* Stack-Pointer Variables */
#define sPointH   0x58E
#define sPointL   0x58F

#define spSAddH   0x80
#define spSAddL   0x81

/* Tasks Identifiers */  
#define  task_a	 0 
#define  task_b	 1 
#define  task_c  2 

/* Properties */
#define  FULL	      1 
#define  EMPTY      0 
#define  ACTIVATE   1
#define  NOACTIVATE 0

/* Tasks Properties */ 
#define  PRIORITY       0
#define  SCHEDULE       1  
#define  AUTOSTART      2
#define  ACTIVATION     3
#define  INTERRUPTED    1
#define  NOINTERRUPTED  0
#define  CONTEXTED      1
#define  NOCONTEXTED    0

/* FIFO Size */
#define  fifoSize	  10 

/*****************************************************************************
 * Objects Definitions                                                       *
 *****************************************************************************/

/* Task Object Definition */ 
typedef struct TASK_ELEMENTS 
{
  unsigned char u8contex;
  unsigned char u8priority;
  unsigned char u8schedule;
  unsigned char u8autostart;
  unsigned char u8activation;
  unsigned char u8state;
  unsigned char u8functionID;
  /* Context variables */
  unsigned int   u16stackPointer;
  unsigned int   u16goBackSP;
  unsigned char  u8aRegister;
  unsigned char  u8hRegister;
  unsigned char  u8xRegister;
  unsigned char  u8ccrRegister;
  unsigned char  u8interrupted;                                              
  unsigned char  u8mailboxID;                              
  unsigned char  u8mailboxData;
  void           (*funcVoid)(void);
} TASK;

/*****************************************************************************
 * Global Variables                                                          *
 *****************************************************************************/ 

/* Context Variables */
extern unsigned char  var     @ 0x380;
extern unsigned int   sp      @ spAddH;
extern unsigned char  aReg    @ aAdd;
extern unsigned char  hReg    @ hAdd;
extern unsigned char  xReg    @ xAdd;
extern unsigned char  ccrReg  @ ccrAdd; 
extern unsigned int   spStak  @ spSAddH;  
extern unsigned int   spInte  @ spIntH;
extern unsigned char  aInte   @ aInt;
extern unsigned char  hInte   @ hInt;
extern unsigned char  xInte   @ xInt;
extern unsigned char  ccrInte @ ccrInt; 
extern unsigned int  brinco   @ varCheck;

/* Stack Pointer Saved */ 
extern unsigned int  goBackSP @ sPointH;
extern unsigned char begining;
extern unsigned char alarmIndex;
extern unsigned char taskIndex;
extern unsigned int  tickCounter;
extern unsigned int  baseTickCounter;

extern unsigned char index;

/* Pointers */
extern TASK  *fptr;  
extern TASK  *lptr;

/* Tasks Buffers */
extern TASK  fifoTask [fifoSize];   
extern unsigned char size;

/* Tasks Prototypes */
extern TASK Task_a;
extern TASK Task_b;
extern TASK Task_c;

extern unsigned char case_probe;

/*****************************************************************************
 * Function Prototypes                                                          *
 *****************************************************************************/

void newConstructorDefault(TASK* thisTask, unsigned char u8thisFunctionID);
void newConstructorID_P_AUSTR(TASK* thisTask, unsigned char u8thisFunctionID,unsigned char u8thisPriority,
unsigned char u8thisAutostart);
void newConstructor(TASK* thisTask, unsigned char u8thisPriority, 
unsigned char u8thisSchedule, unsigned char u8thisAutostart,
unsigned char u8thisActivation, unsigned char u8thisState, 
unsigned char u8thisContex, unsigned char u8thisFunctionID);
unsigned char taskGetFunctionID(TASK* thisTask);
unsigned char taskGetPriority(TASK* thisTask);
unsigned char taskGetSchedule(TASK* thisTask);
unsigned char taskGetAutostart(TASK* thisTask);
unsigned char taskGetActivation(TASK* thisTask);
unsigned char taskGetState(TASK* thisTask);
unsigned char taskGetContex(TASK* thisTask);
void taskSetFunctionID(TASK* thisTask, unsigned char u8thisFunctionID);
void taskSetPriority(TASK* thisTask, unsigned char u8thisPriority);
void taskSetSchedule(TASK* thisTask, unsigned char u8thisSchedule);
void taskSetAutostart(TASK* thisTask, unsigned char u8thisAutostart);
void taskSetActivation(TASK* thisTask, unsigned char u8thisActivation);
void taskSetState(TASK* thisTask, unsigned char u8thisState);
void taskSetContex(TASK* thisTask, unsigned char u8thisContex);
void copyTask(TASK* task2Copy, TASK* taskCopied);
TASK ejectTask(void);
void taskPrioritize(void);
void interruptActivateTask(TASK* thisTask);
void insertTask(TASK* thisTask);

/* Tasks Prototypes for OS */
void TASK_A( void );
void TASK_B( void );
void TASK_C( void );

/* System Function Prototypes */
void activateTask(TASK* thisTask);
void initOS(void);
void terminateTask(TASK* thisTask);
void chainTask(TASK* thisTask);
void goBack(void);
void returnFromInterruption(void); 

/*****************************************************************************
 * Includes                                                                  *
 *****************************************************************************/

#include "test_configuration.h"
#include "bios.h"
#include "Mailboxes.h"
#include "Alarms.h"

#endif  /* _TASKS_H */